require('page/common/header/index.js')
var _order_service = require('service/order_service');
const _util = require('../../../util/util');

var view_order = {
    data: {
        order: {},
        totalPrice: 0,
    },
    init: function(){
        this.loadInfo();
        this.bindEvents();
    },

    loadInfo: function(){
        var orderId = _util.getURLParam('orderId');
        _order_service.getOrderById(
            orderId, 
            orderRes => {
                //将订单的地址信息渲染到页面上
                console.log(orderRes);
                this.renderAddressMsg(orderRes.data);

                //获取订单的地址信息，然后再去获取订单的商品信息
                 _order_service.getItemByOrderId(
                     orderId, 
                     itemRes => {
                         this.renderItemMsg(itemRes.data);

                     }, 
                     errMsg => {

                     }
                )
                
            },
            errMsg => {

            }
        )
    },


    //把订单信息渲染到页面上
    renderAddressMsg: function(order){
        this.data.order = order;
        $("#orderHeader").html(`#${order.orderId} —— ${this.renderTime(order.orderDate)}`)
        $("#cardType").html(order.cardType);
        $("#cardNumber").html(order.creditCard);
        $("#date").val(order.expriyDate);
        $("#billToFirstName").html(order.billToFirstName);
        $("#billToLastName").html(order.billToLastName);
        $("#billAddress1").html(order.billAddress1);
        $("#billAddress2").html(order.billAddress2);
        $("#billCity").html(order.billCity);
        $("#billState").html(order.billState);
        $("#billZip").html(order.billZip);
        $("#billCountry").html(order.billCountry);
        $("#shipToFirstName").html(order.shipToFirstName);
        $("#shipToLastName").html(order.shipToLastName);
        $("#shipAddress1").html(order.shipAddress1);
        $("#shipAddress2").html(order.shipAddress2);
        $("#shipCity").html(order.shipCity);
        $("#shipState").html(order.shipState);
        $("#shipZip").html(order.shipZip);
        $("#shipCountry").html(order.shipCountry);
        $("#courier").html(order.courier);
        $("#status").html(order.status);
    },

    //把订单中的商品信息渲染到页面上
    renderItemMsg: function(itemList){
        var html = `<tr>
        <th>Item ID</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Total Cost</th>
        </tr>`
        var sum = 0;
        for(const element of itemList){
            html += `
                <tr>
                    <td>${element.itemId}</td>
                    <td>${element.descn}</td>
                    <td>${element.quantity}</td>
                    <td>$ ${element.listPrice}</td>
                    <td>$ ${element.totalPrice}
                </tr>
            `
            sum += element.totalPrice;
        }
        this.data.totalPrice = sum;
        html += `<tr>
                    <th colspan="5">
                    Total: <span id="total">$ ${sum}</span>
                    </th>
                </tr>`
        $("#itemTable").html(html);
    },

    //为pay按钮绑定单击事件
    bindEvents: function(){
        var _this = this;
        $("#pay").on("click", function(){   
            window.location.href = `http://localhost:8088/pay/${_this.data.totalPrice}/${_this.data.order.orderId}/${_this.data.order.billToFirstName}/${_this.data.order.billAddress1}`;
        })
    },

    //渲染时间
    renderTime: function(date) {
        var dates = new Date(date).toJSON();
        return new Date(+new Date(dates) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') 
    }

}

module.exports = view_order.init()